#!/bin/sh /etc/rc.common
# Copyright (C) 2017-2023 kooiot.com

USE_PROCD=1
START=96
STOP=1

BASE_DIR=/usr/ioe
SKYNET_DIR=$BASE_DIR/skynet
FREEIOE_DIR=$SKYNET_DIR/ioe

PM_BIN=process-monitor
SKYNET_BIN=$SKYNET_DIR/skynet
STARTUP_SH=$FREEIOE_DIR/startup.sh

if [ -f /etc/os-release ]
then
	source /etc/os-release

	VERSION_ID=`echo $VERSION_ID | sed -e 's/\-snapshot//g'`

	OS_ARCH=$LEDE_ARCH
	if [ $OPENWRT_ARCH ]; then
		OS_ARCH=$OPENWRT_ARCH
	fi

	if [ -f $FREEIOE_DIR/openwrt/$VERSION_ID/$OS_ARCH/process-monitor ]
	then
		PM_BIN=$FREEIOE_DIR/openwrt/$VERSION_ID/$OS_ARCH/process-monitor
	fi
fi

start_service () {
	logger -t "FreeIOE" -p user.notice "Starting..."
	procd_open_instance
	procd_set_param env IOE_RUN_AS_DAEMON=1
	procd_set_param command "$PM_BIN" -M 2 -D "$SKYNET_DIR" -S "sh $STARTUP_SH $BASE_DIR" $SKYNET_BIN ioe/config
	procd_set_param respawn
	procd_close_instance
}

